<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-input v-model="form.id" type="hidden"></el-input>
      <el-input v-model="form.productid" type="hidden"></el-input>
      <el-form-item label="所属产品" prop="productname">
        <el-input v-model="form.productname" :readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="设备名称" prop="devicename">
        <el-input v-model="form.devicename"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="descpt">
        <el-input v-model="form.descpt"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { saveDevice } from '@/api/Product'

  export default {
    name: 'RoleManagementEdit1',
    data() {
      return {
        form: {
          devicename: '',
          id: '',
          descpt: '',
          productid: '',
          productname: '',
        },
        rules: {
          devicename: [
            { required: 1, message: '请输入设备名称', trigger: 'blur' },
            {
              min: 1,
              max: 30,
              message: '长度在 1 到 30 个字符',
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        this.title = '编辑'
        this.form = Object.assign({}, row)
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveDevice(this.form)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
